package com.scalar.chiptrack.adaptor.ikanos.silterra;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.parser.DocumentParser;
import javax.swing.text.html.parser.DTD;
import javax.swing.text.MutableAttributeSet;
import java.util.ArrayList;
import java.io.FileReader;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Aug 31, 2006
 * Time: 5:00:48 PM
 */
public class SilterraShipParser extends HTMLEditorKit.ParserCallback
{

    private SilterraShipInfo shipInfo   = null;
    private ArrayList shipList          = new ArrayList();


    private int m_tableTagCount; //keeps the count of number of table tags encountered
    private boolean m_readyForParsing;  //if set to true indicates that the parser has reached the data to be parsed
    private int m_cellIndex;
    private boolean m_cellOpen;
    private boolean m_invalidRow;

    public SilterraShipParser()
    {
        m_tableTagCount     = 0;
        m_readyForParsing   = false;
        m_cellOpen          = false;
        m_cellIndex         = -1;
        m_invalidRow        = false;
    }

    public ArrayList parse( FileReader reader ) throws Exception
    {
        DTD dtd = DTD.getDTD( "HTML" );
        DocumentParser documentParser = new DocumentParser( dtd );
        documentParser.parse( reader, this, true /*ignore CharSet*/ );

        for ( int i = 0; i < shipList.size(); i++ )
        {
            shipInfo = ( SilterraShipInfo ) shipList.get( i );
        }
        return shipList;
    }

    /**
     *
     * @param t
     * @param a
     * @param pos
     */
    public void handleSimpleTag( HTML.Tag t, MutableAttributeSet a, int pos )
    {

        if ( t == HTML.Tag.TABLE )
        {
            m_tableTagCount++;

            //data is present in third table tag. So setting the variable to true to indicate the start of data
            if ( m_tableTagCount == 25)
            {
                m_readyForParsing = true;

            }
            else if ( m_tableTagCount == 26)
            {
                //this block is entered when the table containing data is parsed
                if ( ( shipInfo != null ) && ( m_invalidRow == false ) )
                {
                    shipList.add( shipInfo );
                }

                m_readyForParsing = false;
            }
        }

       if ( m_readyForParsing )
        {
            if ( ( t == HTML.Tag.TR ))
            {
                if ( ( shipInfo != null ) )
                {
                    shipList.add( shipInfo );
                }

                shipInfo = new SilterraShipInfo();
                m_cellIndex = -1;
                m_invalidRow = false;
            }
        }

        // Got a new cell
        if ( m_readyForParsing )
        {
            if ( ( t == HTML.Tag.TD ) )
            {
                if ( m_cellOpen == false )
                {
                    m_cellIndex++;
                    m_cellOpen = true;
                }
                else
                {
                    m_cellOpen = false;
                }
            }
        }
    }

    /**
     *
     * @param data
     * @param pos
     */
    public void handleText( char[] data, int pos )
    {
        if ( ( m_readyForParsing == false ) || ( shipInfo == null ) )
        {
            return;
        }

        String text = new String( data );

        if ( text == null )
        {
            text = "";
        }
        else
        {
            text = text.trim();
        }

        switch (m_cellIndex)
        {
            case 0:

                if ( !( "".equals( text ) ) && !( "Customer Device ID".equalsIgnoreCase( text.trim() ) ) )
                {
                    shipInfo.setCustomerDeviceID( text.trim() );
                }
                break;
            case 1:

                if ( !( "".equals( text ) ) && !( "FW Device ID".equalsIgnoreCase( text.trim() ) ) )
                {
                    shipInfo.setFwDeviceID( text.trim() );
                }
                break;
			case 2:

                if ( !( "".equals( text ) ) && !( "Lot Start Date".equalsIgnoreCase( text.trim() ) ) )
                {
                    shipInfo.setLotStartDate( text.trim() );
                }
                break;
            case 3:

                if ( !( "".equals( text ) ) && !( "FW Lot ID".equalsIgnoreCase( text.trim() ) ) )
                {
                    shipInfo.setFwLotID( text.trim() );
                }
                break;
			case 4:

                if ( !( "".equals( text ) ) && !( "Lot Type".equalsIgnoreCase( text.trim() ) ) )
                {
                    shipInfo.setLotType( text.trim() );
                }
                break;
            case 5:

                if ( !( "".equals( text ) ) && !( "PO No.".equalsIgnoreCase( text.trim() ) ) )
                {
                    shipInfo.setPoNumber( text.trim() );
                }
                break;
            case 6:

                if ( !( "".equals( text ) ) && !( "Invoice No.".equalsIgnoreCase( text.trim() ) ) )
                {
                    shipInfo.setInvoiceNumber( text.trim() );
                }
                break;
            case 7:

                if ( !( "".equals( text ) ) && !( "Quantity".equalsIgnoreCase( text.trim() ) ) )
                {
                    shipInfo.setQuantity( text.trim() );
                }
                break;

            case 8:

                if ( !( "".equals( text ) ) && !( "Shipment Date".equalsIgnoreCase( text.trim() ) ) )
                {
                    shipInfo.setShipmentDate( text.trim() );
                }
                break;

            case 9:

                if ( !( "".equals( text ) ) && !( "Shipment No.".equalsIgnoreCase( text.trim() ) ) )
                {
                    shipInfo.setShipmentNumber( text.trim() );
                }
                break;

              default:
                break;
        }
    }

    /**
     * Resets the objects state
     */
    public void reset()
    {
        m_tableTagCount     = 0;
        m_readyForParsing   = false;
        m_cellOpen          = false;
        m_cellIndex         = -1;
        m_invalidRow        = false;
    }
}
